<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        class="edit-btn"
        size="mini"
        round
        type="danger"
        plain
        @click="isEdit = !isEdit"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(item, index) in myChannels"
        :key="item.id"
        @click="onMyChannelClick(item, index)"
      >
        <van-icon
          slot="icon"
          v-show="isEdit && !fiexdChannels.includes(item.id)"
          name="clear"
        ></van-icon>
        <span
          class="text"
          :class="{ active: index === myActive }"
          slot="text"
          >{{ item.name }}</span
        >
      </van-grid-item>
    </van-grid>
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid class="recomment-grid" :gutter="10">
      <van-grid-item
        icon="plus"
        class="grid-item"
        v-for="(value, index) in recommendChannele"
        :key="index"
        :text="value.name"
        @click="onAddChannel(value)"
      />
    </van-grid>
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel,
} from "@/api/channel";
import { mapState } from "vuex";
import { setItem } from '@/utils/storage.js'
export default {
  data() {
    return {
      allChannels: [],
      isEdit: false,
      fiexdChannels: [0], // 不需要编辑、删除的频道
    };
  },
  computed: {
    recommendChannele() {
      return this.allChannels.filter((item) => {
        return !this.myChannels.find((obj) => {
          return item.id === obj.id;
        });
      });
    },
    ...mapState(["user"]),
  },
  name: "ChannelEdit",
  props: {
    myChannels: {
      type: Array,
      required: true,
    },
    myActive: {
      type: Number,
      required: true,
    },
  },
  created() {
    this.loadAllChannels();
  },
  methods: {
    async loadAllChannels() {
      try {
        const res = await getAllChannels();
        this.allChannels = res.data.data.channels;
        console.log(res);
      } catch (error) {
        this.$toast("获取所有频道数据失败");
      }
    },
    async deleteChannl(value) {
      try {
        if (this.user) {
          await deleteUserChannel(value.id);
          console.log("删除成功");
        } else {
          localStorage.setItem(
            "TOUTIAO_CHANNELS",
            JSON.stringify(this.myChannels)
          );
        }
      } catch (error) {
        this.$toast("失败");
        console.log(error);
      }
    },
    onMyChannelClick(value, index) {
      // 编辑状态
      if (this.isEdit) {
        // 不允许删除固定频道
        if (this.fiexdChannels.includes(value.id)) {
          return;
        }
        // 删除频道
        this.myChannels.splice(index, 1);
        if (index <= this.myActive) {
          // 让高亮索引-1 传个true 别关闭弹层窗
          this.$emit("update-active", this.myActive - 1, true);
        }
        // 处理持久化
        this.deleteChannl(value);
      } else {
        // 非编辑状态 传过去改变active 且让弹层窗关闭
        this.$emit("update-active", index, false);
      }
    },
    async onAddChannel(channel) {
      // console.log(1);
      this.myChannels.push(channel);
      // 数据持久化处理
      if (this.user) {
        // 已登录，把数据请求接口放到服务器
        try {
          await addUserChannel({
            id: channel.id, // 频道 ID
            seq: this.myChannels.length, // 序号
          });
          console.log("添加成功");
        } catch (error) {
          this.$toast("保存失败，请稍后再试");
        }
      } else {
        // 未登录，把数据存储到本地
        // setItem("TOUTIAO_CHANNELS", this.myChannels);
        setItem("TOUTIAO_CHANNELS",this.myChannels)
      }
    },
  },
};
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;

  .title-text {
    font-size: 32px;
    color: #333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;

      .van-grid-item__text,
      .text {
        font-size: 22px;
        color: #222;
        margin-top: 4px !important;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recomment-grid {
    .grid-item {
      width: 160px;
      height: 86px;
      .van-grid-item__content {
        flex-direction: row;

        .van-icon-plus {
          font-size: 22px;
          line-height: 22px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>